﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
// using Coravel.Mailer.Mail;
using Microsoft.EntityFrameworkCore;

namespace CRM.Models.Email
{
    public class EmailConfig : BaseModel
    {
        private EmailTypeEnum _emailType;
        
        [Required]
        [Comment("授权类型")]
        public EmailTypeEnum EmailType
        {
            get => _emailType;
            set
            {
                _emailType = value;
                InitConfig(this, value);
            }
        }

        public static void InitConfig(EmailConfig emailConfig, EmailTypeEnum emailTypeEnum)
        {
            switch (emailTypeEnum)
            {
                case EmailTypeEnum.QQ:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.qq.com";
                    emailConfig.Port = 25;
                    break;
                case EmailTypeEnum.Netease126:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.126.com";
                    emailConfig.Port = 25;
                    break;
                case EmailTypeEnum.Netease163:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.163.com";
                    emailConfig.Port = 25;
                    break;
                 case EmailTypeEnum.Sina:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.sina.com";
                    emailConfig.Port = 25;
                    break;
                case EmailTypeEnum.Sohu:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.sohu.com";
                    emailConfig.Port = 25;
                    break; 
                case EmailTypeEnum.Gmail:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.gmail.com";
                    emailConfig.Port = 465;
                    break;
                case EmailTypeEnum.Hotmail:
                    emailConfig.Driver = "SMTP";
                    emailConfig.Host = "smtp.live.com";
                    emailConfig.Port = 25;
                    break;

                // case EmailTypeEnum.Other:
                default:
                    // InitSqlObjects.Add(New MailType("21cn.com", "smtp.21cn.com", False, 25))
                    break;
            }
        }
        
        [Comment("id")]
        public int Id { get; set; }
        
        [Comment("UserId")]
        public int UserId { get; set; }
        
        [Comment("GroupId")]
        public int GroupId { get; set; }
        
        [Comment("CompanyId")]
        public int CompanyId { get; set; }
        
        [Comment("OemId")]
        public int OemId { get; set; }

        [Column("EmailGroupId")]
        public int? EmailConfigGroupId { get; set; }
        public virtual EmailConfigGroup EmailConfigGroup { get; set; }
        
        [Comment("协议")]
        public string Driver { get; set; }
        
        [Comment("域名")]
        public string Host { get; set; }
        
        [Comment("端口")]
        public int Port { get; set; }
        
        [Required, EmailAddress]
        [Comment("邮箱地址")]
        public string EmailAddress { get; set; }
        
        [Required]
        [Comment("密钥")]
        public string Secret { get; set; }
         
        // [NotMapped] public MailRecipient EmailAddressRecipient => new MailRecipient(EmailAddress);

        public enum EmailTypeEnum
        {
            Other,
            QQ,
            Netease126,
            Netease163,
            Sina,
            Sohu,
            Gmail,
            Hotmail
        }
    }
}